'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 离开战斗
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var rebelRecordModel = require('../../../models/rebelRecordModel');
var PlayerManager = require('../../../libs/playerManager');
var rebelService = require('../../../services/rebelService');
var rewardService = require('../../../services/rewardService');
var roleService = require('../../../services/roleService');
var rebelConf = require('../../../config/rebelConf');
var taskService = require('../../../services/taskService');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 离开副本
 * 需要上传的数据:
 * 1.已经拆除的建筑
 * 2.已经打过的经济类的建筑
 * 3.要统计的数据
 */
Handler.prototype.exit = function (msg, session, next) {
    var role_id = session.uid;
    var record_id = msg.record_id;
    var pass_wave = msg.pass_wave || 0;
    var data = msg.data || {};
    if (!record_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var rebel_record_model;
    var reward_list = [];
    var rebel_id;
    async.waterfall(
        [
            function (cb) {
                rebelRecordModel.getRecordByAtkRoleId(role_id, record_id, cb);
            },
            function (model, cb) {
                rebel_record_model = model;

                if (!rebel_record_model) {
                    return next(null, {
                        code: Code.REBEL_RECORD_NOT_EXIST
                    });
                }
                rebel_id = rebel_record_model.getRebelId();

                if (!!rebel_record_model.isEnd()) {
                    return next(null, {
                        code: Code.REBEL_HAVE_PASS
                    });
                }
                cb();
            },
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'rebel', 'bag', 'soldier', 'build', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb);
            },
            function (cb) {
                var rebel_model = player_manager.rebel;

                // 判断今天是否已经通关过了
                if (!!rebel_model.isTodayPassRebel(rebel_id)) {
                    return next(null, {
                        code: Code.REBEL_TODAY_HAVE_PASS
                    });
                }

                // 判断这个来袭的之前的难度是否完成
                if (!rebel_model.isPassPreRebel(rebel_id)) {
                    return next(null, {
                        code: Code.REBEL_PRE_NOT_PASS
                    });
                }

                // 添加奖励
                if (pass_wave >= rebelConf.getRebelWaveLength(rebel_id)) {
                    reward_list = rebelService.getRewardList(rebel_id);
                    rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_REBEL, operationType.OPR_REBEL_FIGHT);

                    // 设置数据
                    rebel_record_model.setPassWave(pass_wave);
                    rebel_model.passRebel(rebel_id);
                }

                var lost_soldier_list = data.lost_soldier_list;
                if (!!lost_soldier_list) {
                    _.each(lost_soldier_list, function (soldier_item) {
                        player_manager.soldier.subProductedSoldierNum(soldier_item.soldier_type, soldier_item.num);
                    });
                }
                taskService.addComplete(player_manager);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            rebel_record_model.save();
            return next(null, {
                code: Code.OK,
                result: {
                    pass_wave: pass_wave,
                    total_wave: rebelConf.getRebelWaveLength(rebel_id),
                    reward_list: reward_list
                }
            });
        }
    );
};
